<template>
  <el-container class="home-container">
    <el-header class="el-header">
      <div>
        <img src="../assets/1og2.jpg" />
        <span>个人运动平台</span>
      </div>

      <el-button type="info" @click="logout">安全退出</el-button>
    </el-header>
    <el-container>
      <el-aside width="isCollapse?'64px':'200px'" class="el-aside">
          <div class="toggle-button" @click="toggleCollapase">|||</div>
        <el-menu background-color="#545c64" text-color="#fff" active-text-color="#409eff"
        unique-opened  :collapse="isCollapse" :router="true" :default-active="activePath">
          <el-submenu :index="item.id+''" v-for=" item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.title}}</span>
            </template>
            <el-menu-item :index="it.path+''" v-for=" it in item.slist" :key="it.id" @click="saveNavState(it.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{it.title}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="el-main">
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
        menuList:[],
        isCollapse:false,
        activePath:'/welcome',
    };
  },
  created(){
    this.getMenuList();
    this.activePath=window.sessionStorage.getItem("activePath")
  },
  methods: {
  async  logout() {
      const confirmResult = await this.$confirm('是否退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if(confirmResult=='confirm'){
      window.sessionStorage.clear();
      this.$router.push("/login");
    }
    },
     async getMenuList(){
        const{data:res}=await this.$http.get("/menu")
        this.menuList=res.list;
        // console.log(res)
        // console.log(this.menuList)
     },
     toggleCollapase(){
         this.isCollapse=!this.isCollapse
     },
     saveNavState(activePath){
         window.sessionStorage.setItem("activePath",activePath)
         this.activePath=activePath;
     }
  }
};
</script>
<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  color: #fff;
  font-size: 20px;
  display: flex;
  align-items: center;
}
.el-header div {
  display: flex;
  align-items: center;
}
.el-header span {
  margin-left: 15px;
}
.el-aside {
  background-color: #333744;
}
.el-aside .el-menu{
    border-right:none;
}
.toggle-button{
    background-color: #4A5064;
    font-size:10px;
    line-height:24px;
    color:#fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
.el-main {
  background-color: #eaedf1;
}

img {
  width: 55px;
  height: 55px;
}
</style>